@file:Suppress("unused")

package com.hjh.tool.util

import android.app.Activity
import android.content.Context
import android.content.Intent
import com.hjh.tool.extension.strRes
import com.hjh.res.R
import com.hjq.toast.Toaster

/**
 * 调用系统原生分享工具类。
 *
 * @author vipyinzhiwei
 * @since  2020/5/30
 */
object ShareUtil {

    const val SHARE_MORE = 0
    const val SHARE_QQ = 1
    const val SHARE_WECHAT = 2
    const val SHARE_WEIBO = 3
    const val SHARE_QQ_ZONE = 4
    const val SHARE_WECHAT_MEMORIES = 5

    private fun processShare(activity: Activity, shareContent: String, shareType: Int) {
        when (shareType) {
            SHARE_QQ -> {
                if (!isQQInstalled(activity)) {
                    Toaster.show(R.string.your_phone_does_not_install_qq.strRes(activity))
                    return
                }
                share(
                    activity,
                    shareContent,
                    "com.tencent.mobileqq",
                    "com.tencent.mobileqq.activity.JumpActivity"
                )
            }

            SHARE_WECHAT -> {
                if (!isWechatInstalled(activity)) {
                    Toaster.show(R.string.your_phone_does_not_install_wechat.strRes(activity))
                    return
                }
                share(
                    activity,
                    shareContent,
                    "com.tencent.mm",
                    "com.tencent.mm.ui.tools.ShareImgUI"
                )
            }
            /*SHARE_WECHAT_MEMORIES -> {
                if (!isWechatInstalled(activity)) {
                    Toaster.show(R.string.your_phone_does_not_install_wechat.strRes(activity))
                    return
                }
                share(activity, shareContent, "com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI")
            }*/
            SHARE_WEIBO -> {
                if (!isWeiboInstalled(activity)) {
                    Toaster.show(R.string.your_phone_does_not_install_weibo.strRes(activity))
                    return
                }
                share(
                    activity,
                    shareContent,
                    "com.sina.weibo",
                    "com.sina.weibo.composerinde.ComposerDispatchActivity"
                )
            }

            SHARE_QQ_ZONE -> {
                if (!isQQInstalled(activity)) {
                    Toaster.show(R.string.your_phone_does_not_install_qq_zone.strRes(activity))
                    return
                }
                share(
                    activity,
                    shareContent,
                    "com.qzone",
                    "com.qzonex.module.operation.ui.QZonePublishMoodActivity"
                )
            }

            SHARE_MORE -> {
                share(activity, shareContent)
            }
        }
    }

    private fun share(
        activity: Activity,
        shareContent: String,
        packageName: String,
        className: String
    ) {
        try {
            val shareIntent = Intent(Intent.ACTION_SEND).apply {
                type = "text/plain"
                putExtra(Intent.EXTRA_TEXT, shareContent)
                setClassName(packageName, className)
            }
            activity.startActivity(shareIntent)
        } catch (e: Exception) {
            // Toaster.show(R.string.share_unknown_error))
            Toaster.show("暂不支持此分享")
        }
    }

    private fun share(activity: Activity, shareContent: String) {
        val shareIntent = Intent(Intent.ACTION_SEND).apply {
            type = "text/plain"
            putExtra(Intent.EXTRA_TEXT, shareContent)
        }
        activity.startActivity(
            Intent.createChooser(
                shareIntent,
                R.string.share_to.strRes(activity)
            )
        )
    }

    /**
     * 调用系统原生分享
     *
     * @param shareContent 分享内容
     * @param shareType SHARE_MORE=0，SHARE_QQ=1，SHARE_WECHAT=2，SHARE_WEIBO=3，SHARE_QQ_ZONE=4
     */
    fun share(activity: Activity, shareContent: String, shareType: Int) {
        processShare(activity, shareContent, shareType)
    }


    /**
     * 判断手机是否安装了QQ。
     */
    fun isQQInstalled(context: Context) =
        ApkInstallUtil.isInstalled(context, "com.tencent.mobileqq")

    /**
     * 判断手机是否安装了微信。
     */
    fun isWechatInstalled(context: Context) =
        ApkInstallUtil.isInstalled(context, "com.tencent.mm")

    /**
     * 判断手机是否安装了微博。
     * */
    fun isWeiboInstalled(context: Context) =
        ApkInstallUtil.isInstalled(context, "com.sina.weibo")
}